package com.example.demo.controller;

import com.example.demo.entity.Book;
import com.example.demo.result.Result;
import com.example.demo.result.ResultFactory;
import com.example.demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/library")
public class BookController {
    @Autowired
    BookService bookService;

    @GetMapping("/books")
    public Result selectBook(){
        List<Book> books=bookService.selectBook();
        return ResultFactory.buildSuccessResult(books);
    }

    @GetMapping("/books1")
    public Result selectBook1(){
        List<Book> books=new ArrayList<>();
        Book book=bookService.selectById("519");
        Book book1=bookService.selectById("554");
        Book book2=bookService.selectById("73");
        Book book3=bookService.selectById("142");

        Book book4=bookService.selectById("484");
        Book book5=bookService.selectById("503");
        Book book6=bookService.selectById("753");
        Book book7=bookService.selectById("220");
        Book book8=bookService.selectById("357");
        books.add(book);
        books.add(book1);
        books.add(book2);
        books.add(book3);
        books.add(book4);
        books.add(book5);
        books.add(book6);
        books.add(book7);
        books.add(book8);
        return ResultFactory.buildSuccessResult(books);
    }

    @PostMapping("/selectBN/{name}")
    public Result selectByName(@PathVariable("name") String name){
        List<Book> books=bookService.selectByName(name);
        return ResultFactory.buildSuccessResult(books);
    }

    @PostMapping("/selectBT/{type}")
    public Result selectByType(@PathVariable("type") int type){
        List<Book> books=bookService.selectByType(type);
        return ResultFactory.buildSuccessResult(books);
    }

    @PostMapping("/editB/{id}")
    public Result editB(@PathVariable("id") String id){
        Book book=bookService.selectById(id);
        return ResultFactory.buildSuccessResult(book);
    }

    @PostMapping("/updateB")
    @ResponseBody
    public Result updateB(@RequestBody Map<String,String> map){
        Book book=new Book();
        book.setAuthor(map.get("author"));
        book.setBookId(Integer.parseInt(map.get("bookId")));
        book.setBNumber(Integer.parseInt(map.get("bnumber")));
        book.setBookName(map.get("bookName"));
        book.setImg(map.get("img"));
        book.setPrice(map.get("price"));
        book.setPublisher(map.get("publisher"));
        book.setSummary(map.get("summary"));
        book.setTypeId(Integer.parseInt(map.get("typeId")));
        bookService.updateBook(book);
        return ResultFactory.buildSuccessResult("1");
    }

    @PostMapping("/addB")
    @ResponseBody
    public Result addB(@RequestBody Map<String,String> map){
        Book book=new Book();
        book.setAuthor(map.get("Author"));
        book.setBNumber(Integer.parseInt(map.get("BNumber")));
        book.setBookName(map.get("BookName"));
        book.setImg(map.get("Img"));
        book.setPrice(map.get("Price"));
        book.setPublisher(map.get("Publisher"));
        book.setSummary(map.get("Summary"));
        book.setTypeId(Integer.parseInt(map.get("TypeId")));
        bookService.addBook(book);
        return ResultFactory.buildSuccessResult("1");
    }

    @PostMapping("/delB/{id}")
    public Result delB(@PathVariable("id") String id){
        bookService.delBook(id);
        return ResultFactory.buildSuccessResult(id);
    }

}
